package com.pdf.service;


import cn.afterturn.easypoi.cache.manager.POICacheManager;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.pdf.entity.Test;
import com.pdf.entity.TestExport;
import com.pdf.utils.CommonDownloadUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

import static com.pdf.utils.WordToPdfUtil.docToPdf;

/**
 * @author PC
 * @version 1.0
 * @description: TODO
 * @date 2024/7/9 11:24
 */
@Slf4j
@Service
public class DocToPdfService {

    public void docToPdfExport(HttpServletResponse response) {
        File destTemplateFile = null;
        File resultFile = null;
        File filePdf = null;
        try {
            // 读取模板流
            InputStream inputStream = POICacheManager.getFile("test.docx");
            // 创建一个临时模板
            destTemplateFile = FileUtil.writeFromStream(inputStream, FileUtil.file(FileUtil.getTmpDir() +
                    File.separator + "test.docx"));
            //做数据
            Map<String, Object> map = getStringObjectMap();
            // 生成doc
            XWPFDocument doc = WordExportUtil.exportWord07(destTemplateFile.getAbsolutePath(), map);
            // 生成临时导出文件
            resultFile = FileUtil.file(FileUtil.getTmpDir() + File.separator +  DateUtil.now().replaceAll("[-: ]", "") + ".docx");
            // 写入
            BufferedOutputStream outputStream = FileUtil.getOutputStream(resultFile);
            doc.write(outputStream);
            outputStream.close();
            /****************** word to pdf ******************/
            //生成临时配置pdf文件
            String temPdfFile = FileUtil.getTmpDir() + File.separator +  DateUtil.now().replaceAll("[-: ]", "") + ".pdf";
            //word转pdf
            filePdf = docToPdf(resultFile.getPath(), temPdfFile);
            // 下载PDF
            CommonDownloadUtil.download(filePdf, response);
            /****************** word to pdf ******************/
            // 下载
            // CommonDownloadUtil.download(resultFile, response);
        } catch (Exception e) {
            log.error(">>> 导出锅炉房基本情况失败：", e);
        } finally {
            // 删除临时文件
            if(ObjectUtil.isNotEmpty(destTemplateFile)) {
                FileUtil.del(destTemplateFile);
            }
            if(ObjectUtil.isNotEmpty(resultFile)) {
                FileUtil.del(resultFile);
            }
            if (ObjectUtil.isNotEmpty(filePdf)){
                FileUtil.del(filePdf);
            }
        }

    }

    private static Map<String, Object> getStringObjectMap() {
        TestExport testExport = new TestExport();
        testExport.setName("张三");
        testExport.setAge(18);
        Map<String, Object> map = BeanUtil.beanToMap(testExport);
        ArrayList<Test> tests = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Test test = new Test();
            test.setTestOne("测试01"+i);
            test.setTestTwo("测试02"+i);
            test.setTestThree("这是一段测试的文字03"+i);
            ArrayList<String> strings = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                if (j%2 == 0){
                    strings.add("☑" + j+1+"测试  ");
                }else {
                    strings.add("□" + j+1+".不测试  ");
                }
            }
            test.setTestFour(strings.toString().replace(",", " ").replace("[", "").replace("]", " "));
            tests.add(test);
        }
        map.put("testList", tests);
        return map;
    }
}
